﻿using System;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data
{
    public abstract class ItemCache<T> : EntityCache<T>, IItemCache<T>
        where T : class, IItemData
    {
        public ItemCache(ICacheManager cache)
            : base(cache)
        {
        }

        IItemData IItemCache.FindByName(string itemName)
        {
            return this.FindByName(itemName);
        }

        public T FindByName(string itemName)
        {
            return FindByKey("Name", itemName);
        }

        IItemData IItemCache.FindByAlias(string itemAlias)
        {
            return this.FindByAlias(itemAlias);
        }

        public T FindByAlias(string itemAlias)
        {
            return FindByKey("Alias", itemAlias);
        }

        /// <summary>
        /// Adds the instance to the cache
        /// </summary>
        /// <param name="instance">Object add to the cache</param>
        /// <returns><paramref name="instance"/> value<see langword="null" /></returns>
        /// <exception cref="ArgumentNullException">If <paramref name="instance"/> is <see langref="null"/></exception>
        /// <exception cref="ArgumentException">If <paramref name="instance"/> is not <see cref="IItemData"/> type or if the item Name is <see langref="null"/> or empty</exception>
        public override T Add(T instance)
        {
            if (instance == null)
                throw new ArgumentNullException();
            if (instance.Name == null || instance.Name == "")
                throw new ArgumentException();
            base.Add(instance);
            AddKey(instance.GlobalId, "Name", instance.Name);
            AddKey(instance.GlobalId, "Alias", instance.AliasName);
            return instance;
        }
        /// <summary>
        /// Gets cached search collection
        /// </summary>
        /// <param name="searchText">search text</param>
        /// <returns>The data object global ids collection if found or returns null</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="searchText"/> is <see langref="null"/></exception>
        public string[] Search(string searchText)
        {
            return FindCollection("Search", searchText);
        }
        /// <summary>
        /// Gets cached search collection
        /// </summary>
        /// <param name="searchText">search text</param>
        /// <param name="collection">The data object global ids collection</param>
        /// <returns><paramref name="collection"/> value<see langword="null" /></returns>
        /// <exception cref="ArgumentNullException">If <paramref name="searchText"/> is <see langref="null"/></exception>
        public string[] AddSearch(string searchText, string[] collection)
        {
            AddCollection("Search", searchText, collection);
            return collection;
        }


    }
}
